package com.spring.bean.page;

import java.io.Serializable;

/**
 * @author JHC
 * 计算起始页和终止页
 */
public class PageIndex implements Serializable {
	
	private long startIndex;
	private long endIndex;

	public PageIndex(long startIndex, long endIndex) {
		this.startIndex = startIndex;
		this.endIndex = endIndex;
	}

	public PageIndex() {}

	public long getStartIndex() {
		return startIndex;
	}

	public void setStartIndex(long startIndex) {
		this.startIndex = startIndex;
	}

	public long getEndIndex() {
		return endIndex;
	}

	public void setEndIndex(long endIndex) {
		this.endIndex = endIndex;
	}

	/**
	 * 取得页面显示的起始索引和结束索引
	 * @param viewPagecode 页面上显示的页码总数
	 * @param currentPage 当前页
	 * @param totalpage 总页数
	 * @return
	 */
	public static PageIndex getPageIndex(int viewPagecode, int currentPage, long totalpage){
		long startIndex = currentPage - (viewPagecode%2==0?viewPagecode/2-1:viewPagecode/2);
		long endIndex = currentPage+1;
		if(startIndex <= 0){
			startIndex = 1;
			if(totalpage > viewPagecode){
				endIndex = viewPagecode;
			} else {
				endIndex = totalpage;
			}
		}
		if(endIndex > totalpage){
			endIndex = totalpage;
			if((endIndex - viewPagecode) > 0){
				startIndex = endIndex - viewPagecode + 1;
			} else {
				startIndex = 1;
			}
		}
		return new PageIndex(startIndex, endIndex);
	}
}
